from django.shortcuts import render
import json
from django.http import JsonResponse
from django.shortcuts import render
from .models import Stumodel
from django.views import View
from pyexcel_xlsx import get_data

class StuView(View):
    def get(self,req):
        pagesize=int(req.GET.get("size"))
        pagenum=int(req.GET.get("page"))
        start=(pagenum-1)*pagesize
        end=start+pagesize
        if req.GET.get('name'):
            data = Stumodel.objects.filter(name__contains=req.GET.get('name'))[start:end].values()
            total = len(Stumodel.objects.filter(name__contains=req.GET.get('name')))
        else:
            data=Stumodel.objects.values()[start:end]
            total=len(Stumodel.objects.values())
        return JsonResponse({"code":200,"data":list(data),"total":total})
    def post(self,req):
        if req.FILES.get('file'):
            f=req.FILES.get('file')
            data = get_data(f)
            # print(type(data))  # 可以看到, 类型为collections.OrderedDict
            s=json.dumps(data)
            d=json.loads(s)
            sheet=d['Sheet1']
            # print(d)
            for i in sheet[1:]:
                Stumodel.objects.create(name=i[0],city=i[1],level=i[2],parphone=i[3],phone=i[4],shichang=i[5],age=i[6],sex=i[7],degree=i[8],classbj=i[9])
        else:
            d=json.loads(req.body)
            u=Stumodel.objects.create(**d)
        return JsonResponse({"message":"添加成功","code":200})
    def put(self,req,id):
        d=json.loads(req.body)
        Stumodel.objects.filter(id=id).update(name=d['name'],classbj=d['classbj'],city=d['city'],level=d['level'],parphone=d['parphone'],phone=d['phone'],shichang=d['shichang'])
        return JsonResponse({"message":"修改成功","code":200})
    def delete(self,req,id):
        Stumodel.objects.filter(id=id).delete()
        return JsonResponse({"message":"删除成功","code":200})
# 性别环形图
def sexchart(req):
    man=Stumodel.objects.filter(sex='男').values()
    mantotal=len(man)
    women=Stumodel.objects.filter(sex='女').values()
    womentotal=len(women)
    data= [
            {'value': womentotal, 'name': '女'},
            {'value': mantotal, 'name': '男'}

        ]
    return JsonResponse({"message": "获取成功", "code": 200,'data':data})
# 折线图学历
def degchart(req):
    zerofour1 = Stumodel.objects.filter(degree='初中').values()
    zerofour2 = Stumodel.objects.filter(degree='高中').values()
    zerofour3 = Stumodel.objects.filter(degree='专科').values()
    zerofour4 = Stumodel.objects.filter(degree='本科').values()
    one=len(zerofour1)
    two=len(zerofour2)
    three=len(zerofour3)
    four = len(zerofour4)

    data=[one, two, three,four]

    return JsonResponse({"message": "获取成功", "code": 200, 'data': data})

def agechart(req):
    one = len(Stumodel.objects.filter(age__range=[15,17]).values())
    two = len(Stumodel.objects.filter(age__range=[18, 20]).values())
    three = len(Stumodel.objects.filter(age__range=[21, 23]).values())
    four = len(Stumodel.objects.filter(age__range=[24, 27]).values())
    five = len(Stumodel.objects.filter(age__range=[28, 30]).values())


    data= [five, four, three, two, one]

    return JsonResponse({"message": "获取成功", "code": 200, 'data': data})
def mapchart(req):


    data=[]
    d = Stumodel.objects.values()
    dict={}
    for i in d:
        key=i['city'].split('省')[0]
        dict[key]=dict.get(key,0)+1
    for i in dict:
        data.append({'name':i,'value':dict[i]})
    return JsonResponse({"message": "获取成功", "code": 200, 'data': data})
